import Cookies from 'vue-cookie'
import '../../public/config'

const TokenKey = window.SITE_CONFIG.tokenKey === undefined ? 'token' : window.SITE_CONFIG.tokenKey

// 获取tokenKey
export function getTokenKey() {
  return TokenKey
}

// 获取token值
export function getToken() {
  const token = Cookies.get(TokenKey)
  return token == null ? sessionStorage.getItem(TokenKey) : token
}

// 设置token
export function setToken(token) {
  sessionStorage.setItem(TokenKey, token)
  const path = window.SITE_CONFIG['baseUrl'].substring(window.SITE_CONFIG['baseUrl'].lastIndexOf('/'), window.SITE_CONFIG['baseUrl'].length)
  return Cookies.set(TokenKey, token, '30MIN', path)
}

// 移除token
export function removeToken() {
  sessionStorage.removeItem(TokenKey)
  return Cookies.delete(TokenKey)
}
